package org.one.stone.soup.grfxML.plugin.builder;

import org.one.stone.soup.grfxML.DataChildren;
import org.one.stone.soup.grfxML.DataNode;
import org.one.stone.soup.grfxML.DataState;
import org.one.stone.soup.grfxML.DataString;
import org.one.stone.soup.grfxML.GrfxMLEngine;
import org.one.stone.soup.grfxML.plugin.SimplePlugin;
import org.one.stone.soup.grfxML.plugin.grfxMLCaster;
import org.one.stone.soup.mjdb.data.field.DataLinker;
import org.one.stone.soup.mjdb.data.field.LockException;
import org.one.stone.soup.stringhelper.StringGenerator;

public class SuperStringLinker extends SimplePlugin {

	private static final int ARG_LINK_A = 0;
	private static final int ARG_LINK_B = 1;

	private static final int ARG_TARGET = 2;

	private static final int ARG_PROTOTYPE_NAME = 3;

	private static final int ARG_LINK_A_NAME = 4;
	private static final int ARG_LINK_B_NAME = 5;

	DataState linkA = new DataState();
	DataState linkB = new DataState();

	DataChildren target;

	DataString prototypeName = new DataString();

	DataString linkAName = new DataString();
	DataString linkBName = new DataString();

	DataNode nodeA;
	DataNode nodeB;
	DataNode prototype;
/**
 * SuperString constructor comment.
 */
public SuperStringLinker(GrfxMLEngine engine) {
	super(engine);

	target = new DataChildren(getEngine());
}
/**
 * initialize method comment.
 */
public void initialize()
{
}
/**
 * process method comment.
 */
public void process()
{
 try{

	if(getEngine().getAPI().getMouse().getButton2Pressed().getValue()==true)
	{
		if(linkA.getValue()==true)
		{
			if(getEngine().getAPI().getMouse().getOver()!=null)
			{
				System.out.println( getEngine().getAPI().getMouse().getOver().getId() );

				nodeA = (DataNode)getEngine().getAPI().getMouse().getOver();
				linkA.setValue( false,this );

				getEngine().getAPI().setAutoCommit(true);
				prototype = getEngine().getAPI().buildPrototype(prototypeName.getValue(),"link"+StringGenerator.generateUniqueId(),target);

				((DataString)getEngine().getAPI().getField(prototype.getId()+"."+linkAName.getValue())).setValue( nodeA.getId()+".next",this );

				linkB.setValue( true,this );
			}
		}
		else if(linkB.getValue()==true)
		{
				if(getEngine().getAPI().getMouse().getOver()!=null)
				{
					System.out.println( getEngine().getAPI().getMouse().getOver().getId() );

					nodeB = (DataNode)getEngine().getAPI().getMouse().getOver();

					((DataString)getEngine().getAPI().getField(prototype.getId()+"."+linkBName.getValue())).setValue( nodeB.getId(),this );

					linkB.setValue( false,this );
					linkA.setValue( true,this );
				}
		}
	}

 }catch(LockException le){}
}
/**
 * register method comment.
 */
public void register(DataLinker slinkBre)
{
	linkA	=	grfxMLCaster.cast(linkA,getArg(linkA,ARG_LINK_A,slinkBre));
	linkB 	=	grfxMLCaster.cast(linkB,getArg(linkB,ARG_LINK_B,slinkBre));

	target	=	grfxMLCaster.cast(target,getArg(target,ARG_TARGET,slinkBre));

	prototypeName	=	grfxMLCaster.cast(prototypeName,getArg(prototypeName,ARG_PROTOTYPE_NAME,slinkBre));

	linkAName	=	grfxMLCaster.cast(linkAName,getArg(linkAName,ARG_LINK_A_NAME,slinkBre));
	linkBName 	=	grfxMLCaster.cast(linkBName,getArg(linkBName,ARG_LINK_B_NAME,slinkBre));

}
/**
 * replace method comment.
 */
public void replace(org.one.stone.soup.mjdb.data.field.Field oldObj, org.one.stone.soup.mjdb.data.field.Field newObj)
{
	linkA	=	grfxMLCaster.replace(linkA,oldObj,newObj);
	linkB	=	grfxMLCaster.replace(linkB,oldObj,newObj);

	target	=	grfxMLCaster.replace(target,oldObj,newObj);

	prototypeName	=	grfxMLCaster.replace(prototypeName,oldObj,newObj);

	linkAName	=	grfxMLCaster.replace(linkAName,oldObj,newObj);
	linkBName	=	grfxMLCaster.replace(linkBName,oldObj,newObj);
}
/**
 * stop method comment.
 */
public void stop() {}
}
